﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class VendorBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IEdiLogDA edilogDA = DataAccess.CreateEdiLogDA();
        private PubParasInfo pubParasInfo;
        private static readonly IVendorDA VendorDA = DataAccess.CreateVendorDA();

        public VendorBLL()
        {
            this.pubParasInfo = null;
        }

        public VendorBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public VendorBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> vendorIDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<VendorInfo> vendorInfos = new List<VendorInfo>();
                VendorInfo vendorInfo = null;
                foreach (string id in vendorIDS)
                {
                    vendorInfo = VendorDA.GetVendorByID(dataBase, tran, id, this.dataBaseID[0], this.pubParasInfo);
                    vendorInfos.Add(vendorInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (VendorDA.DeleteVendor(dataBase, tran, vendorIDS, this.dataBaseID) > 0)
                {
                    for (int i = 0; i < vendorIDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<VendorInfo>(dataBase, tran, vendorInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "VENDOR", this.dataBaseID);
                        temp = temp + vendorIDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return VendorDA.GetAllVendor(this.dataBaseID[0], this.pubParasInfo);
        }

        public object GetInfoByID(string VendorID)
        {
            return VendorDA.GetVendorByID(VendorID, this.dataBaseID[0], this.pubParasInfo);
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            VendorQueryEntity VendorQuery = obj as VendorQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(VendorQuery.VendorID))
            {
                sqlWhere.Add(" V.VENDOR_ID " + VendorQuery.VendorID);
            }
            if (!string.IsNullOrEmpty(VendorQuery.Company))
            {
                sqlWhere.Add(" V.COMPANY " + VendorQuery.Company);
            }
            if (!string.IsNullOrEmpty(VendorQuery.Description))
            {
                sqlWhere.Add(" V.DESCRIPTION " + VendorQuery.Description);
            }
            if (!string.IsNullOrEmpty(VendorQuery.City))
            {
                sqlWhere.Add(" V.CITY " + VendorQuery.City);
            }
            if (!string.IsNullOrEmpty(VendorQuery.IsActive))
            {
                sqlWhere.Add(" V.IS_ACTIVE " + VendorQuery.IsActive);
            }
            return VendorDA.GetVendorByQueryList(sqlWhere, VendorQuery, this.dataBaseID[0], this.pubParasInfo);
        }

        public string Insert(object obj)
        {
            string VendorID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                VendorInfo vendor = obj as VendorInfo;
                if (VendorDA.CheckVendorIDUnique(dataBase, tran, vendor.VendorID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2130_001", this.pubParasInfo.UserLanguage, new List<string> { vendor.VendorID });
                }
                vendor.UpdatedBy = vendor.CreatedBy;
                if (VendorDA.InsertVendor(dataBase, tran, vendor, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<VendorInfo>(dataBase, tran, null, vendor, AuditAction.Insert, this.pubParasInfo.UserID, "VENDOR", this.dataBaseID);
                    VendorID = vendor.VendorID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return VendorID;
        }

        public string InsertVendorByWebService(List<VendorInfo> vendorInfo)
        {
            EdiLogInfo edilogInfo;
            string vendorID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            VendorInfo vendorEx = null;
            List<EdiLogInfo> edilogInfoList = new List<EdiLogInfo>();
            try
            {
                foreach (VendorInfo info in vendorInfo)
                {
                    vendorEx = info;
                    if (VendorDA.CheckAuthorizationVendorIDUserID(dataBase, tran, info.VendorID, this.dataBaseID[0], this.pubParasInfo))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F2120_001", this.pubParasInfo.UserLanguage, new List<string> { info.VendorID });
                    }
                    VendorDA.InsertVendor(dataBase, tran, info, this.dataBaseID);
                    edilogInfo = new EdiLogInfo {
                        WhID = "",
                        LogDate = DateTime.Now,
                        IsSuccess = "成功",
                        LogDescr = "承运商" + info.VendorID + "插入成功",
                        UserID = "CEAWebService",
                        Udf1 = "新增",
                        Udf2 = "承运商代码:" + info.VendorID,
                        Udf3 = "承运商资料",
                        Udf4 = "国家代码:" + info.Country
                    };
                    edilogInfoList.Add(edilogInfo);
                }
                tran.Commit();
                vendorID = "OK";
            }
            catch (Exception ex)
            {
                tran.Rollback();
                edilogInfoList = new List<EdiLogInfo>();
                edilogInfo = new EdiLogInfo {
                    WhID = "",
                    LogDate = DateTime.Now,
                    IsSuccess = "异常",
                    LogDescr = "承运商" + vendorEx.VendorID + "插入失败:" + ex.Message,
                    UserID = "CEAWebService",
                    Udf1 = "新增",
                    Udf2 = "承运商代码:" + vendorEx.VendorID,
                    Udf3 = "承运商资料",
                    Udf4 = "国家代码:" + vendorEx.Country
                };
                vendorID = ex.Message;
            }
            finally
            {
                edilogDA.InsertEdiLogs(edilogInfoList);
                conn.Close();
            }
            return vendorID;
        }

        public string Update(object obj)
        {
            string VendorID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                VendorInfo vendor = obj as VendorInfo;
                vendor.UpdatedDate = DateTime.Now;
                VendorInfo oldVendorInfo = VendorDA.GetVendorByID(dataBase, tran, vendor.VendorID, this.dataBaseID[0], this.pubParasInfo);
                if (VendorDA.UpdateVendor(dataBase, tran, vendor, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<VendorInfo>(dataBase, tran, oldVendorInfo, vendor, AuditAction.Update, this.pubParasInfo.UserID, "VENDOR", this.dataBaseID);
                    VendorID = vendor.VendorID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return VendorID;
        }

        public string UpdateVendorByWebService(List<VendorInfo> vendorInfo)
        {
            EdiLogInfo edilogInfo;
            string customerID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            VendorInfo vendorEx = null;
            List<EdiLogInfo> edilogInfoList = new List<EdiLogInfo>();
            string strVendorID = string.Empty;
            string strCompany = string.Empty;
            try
            {
                foreach (VendorInfo info in vendorInfo)
                {
                    strVendorID = info.VendorID;
                    vendorEx = VendorDA.GetVendorWebServiceByID(dataBase, tran, info.VendorID, this.dataBaseID[0], this.pubParasInfo);
                    if (vendorEx == null)
                    {
                        throw new Exception("未找到匹配数据源");
                    }
                    strCompany = info.Country;
                    if (!string.IsNullOrEmpty(info.IsActive))
                    {
                        vendorEx.IsActive = info.IsActive;
                    }
                    if (!string.IsNullOrEmpty(info.Udf1))
                    {
                        vendorEx.Udf1 = info.Udf1;
                    }
                    if (!string.IsNullOrEmpty(info.Udf2))
                    {
                        vendorEx.Udf2 = info.Udf2;
                    }
                    if (!string.IsNullOrEmpty(info.Udf3))
                    {
                        vendorEx.Udf3 = info.Udf3;
                    }
                    if (!string.IsNullOrEmpty(info.Udf4))
                    {
                        vendorEx.Udf4 = info.Udf4;
                    }
                    VendorDA.UpdateVendor(dataBase, tran, vendorEx, this.dataBaseID);
                    edilogInfo = new EdiLogInfo {
                        WhID = "",
                        LogDate = DateTime.Now,
                        IsSuccess = "成功",
                        LogDescr = "承运商" + vendorEx.VendorID + "更新成功",
                        UserID = "CEAWebService",
                        Udf1 = "更新",
                        Udf2 = "承运商代码:" + vendorEx.VendorID,
                        Udf3 = "承运商资料",
                        Udf4 = "国家代码:" + vendorEx.Country
                    };
                    edilogInfoList.Add(edilogInfo);
                }
                tran.Commit();
                customerID = "OK";
            }
            catch (Exception ex)
            {
                tran.Rollback();
                edilogInfoList = new List<EdiLogInfo>();
                edilogInfo = new EdiLogInfo {
                    WhID = "",
                    LogDate = DateTime.Now,
                    IsSuccess = "异常",
                    UserID = "CEAWebService",
                    Udf1 = "更新",
                    Udf3 = "承运商资料"
                };
                if (vendorEx != null)
                {
                    edilogInfo.LogDescr = "客户" + vendorEx.VendorID + "更新失败:" + ex.Message;
                    edilogInfo.Udf2 = "承运商代码:" + vendorEx.VendorID;
                    edilogInfo.Udf4 = "国家代码:" + vendorEx.Country;
                }
                else
                {
                    edilogInfo.LogDescr = "客户" + strVendorID + "更新失败:" + ex.Message;
                    edilogInfo.Udf2 = "承运商代码:" + strVendorID;
                    edilogInfo.Udf4 = "国家代码:" + strCompany;
                }
                customerID = ex.Message;
            }
            finally
            {
                edilogDA.InsertEdiLogs(edilogInfoList);
                conn.Close();
            }
            return customerID;
        }

        public string VendorImportByData(string[][] ImportByData, string IsUpdate)
        {
            string vendorIDTrue = string.Empty;
            string vendorIDFalse = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 1; ImportByData.Length > i; i++)
                {
                    VendorInfo vendor = new VendorInfo();
                    for (int n = 0; ImportByData[i].Length > n; n++)
                    {
                        try
                        {
                            switch (ImportByData[0][n])
                            {
                                case "供应商代码":
                                {
                                    if (!string.IsNullOrEmpty(ImportByData[i][n]))
                                    {
                                        vendor.VendorID = ImportByData[i][n];
                                    }
                                    continue;
                                }
                                case "供应商名称":
                                {
                                    vendor.Company = ImportByData[i][n];
                                    continue;
                                }
                                case "描述":
                                {
                                    vendor.Description = ImportByData[i][n];
                                    continue;
                                }
                                case "增值税号":
                                {
                                    vendor.Vat = ImportByData[i][n];
                                    continue;
                                }
                                case "城市":
                                {
                                    vendor.City = ImportByData[i][n];
                                    continue;
                                }
                                case "省份":
                                {
                                    vendor.State = ImportByData[i][n];
                                    continue;
                                }
                                case "邮编":
                                {
                                    vendor.Zip = ImportByData[i][n];
                                    continue;
                                }
                                case "国家":
                                {
                                    vendor.Country = ImportByData[i][n];
                                    continue;
                                }
                                case "ISO国家码":
                                {
                                    vendor.CountryCode = ImportByData[i][n];
                                    continue;
                                }
                                case "地址行":
                                {
                                    vendor.Address = ImportByData[i][n];
                                    continue;
                                }
                                case "联系人1":
                                {
                                    vendor.Contact1 = ImportByData[i][n];
                                    continue;
                                }
                                case "电话1":
                                {
                                    vendor.Phone1 = ImportByData[i][n];
                                    continue;
                                }
                                case "传真1":
                                {
                                    vendor.Fax1 = ImportByData[i][n];
                                    continue;
                                }
                                case "联系人1电子邮件":
                                {
                                    vendor.Email1 = ImportByData[i][n];
                                    continue;
                                }
                                case "联系人2":
                                {
                                    vendor.Contact2 = ImportByData[i][n];
                                    continue;
                                }
                                case "电话2":
                                {
                                    vendor.Phone2 = ImportByData[i][n];
                                    continue;
                                }
                                case "传真2":
                                {
                                    vendor.Fax2 = ImportByData[i][n];
                                    continue;
                                }
                                case "联系人2电子邮件":
                                {
                                    vendor.Email2 = ImportByData[i][n];
                                    continue;
                                }
                                case "B城市":
                                {
                                    vendor.BCity = ImportByData[i][n];
                                    continue;
                                }
                                case "B省份":
                                {
                                    vendor.BState = ImportByData[i][n];
                                    continue;
                                }
                                case "B邮编":
                                {
                                    vendor.BZip = ImportByData[i][n];
                                    continue;
                                }
                                case "B国家":
                                {
                                    vendor.BCountry = ImportByData[i][n];
                                    continue;
                                }
                                case "BISO国家码":
                                {
                                    vendor.BCountryCode = ImportByData[i][n];
                                    continue;
                                }
                                case "B账单地址":
                                {
                                    vendor.BAddress = ImportByData[i][n];
                                    continue;
                                }
                                case "B账单联系人1":
                                {
                                    vendor.BContact1 = ImportByData[i][n];
                                    continue;
                                }
                                case "B电话1":
                                {
                                    vendor.BPhone1 = ImportByData[i][n];
                                    continue;
                                }
                                case "B传真1":
                                {
                                    vendor.BFax1 = ImportByData[i][n];
                                    continue;
                                }
                                case "B电子邮件1":
                                {
                                    vendor.BEmail1 = ImportByData[i][n];
                                    continue;
                                }
                                case "B账单联系人2":
                                {
                                    vendor.BContact2 = ImportByData[i][n];
                                    continue;
                                }
                                case "B电话2":
                                {
                                    vendor.BPhone2 = ImportByData[i][n];
                                    continue;
                                }
                                case "B传真2":
                                {
                                    vendor.BFax2 = ImportByData[i][n];
                                    continue;
                                }
                                case "B电子邮件2":
                                {
                                    vendor.BEmail2 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段1":
                                {
                                    vendor.Udf1 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段2":
                                {
                                    vendor.Udf2 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段3":
                                {
                                    vendor.Udf3 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段4":
                                {
                                    vendor.Udf4 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段5":
                                {
                                    vendor.Udf5 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段6":
                                {
                                    vendor.Udf6 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段7":
                                {
                                    vendor.Udf7 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段8":
                                {
                                    vendor.Udf8 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段9":
                                {
                                    vendor.Udf9 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段10":
                                {
                                    vendor.Udf10 = ImportByData[i][n];
                                    continue;
                                }
                                case "备注":
                                {
                                    vendor.Remark = ImportByData[i][n];
                                    continue;
                                }
                                case "创建人":
                                {
                                    vendor.CreatedBy = ImportByData[i][n];
                                    continue;
                                }
                            }
                        }
                        catch
                        {
                            if (!string.IsNullOrEmpty(vendor.VendorID))
                            {
                                vendorIDFalse = vendorIDFalse + vendor.VendorID + "（数据内容异常,导入失败）;/r/n";
                            }
                        }
                    }
                    vendor.IsActive = "Y";
                    vendor.UpdatedBy = vendor.CreatedBy;
                    if (!string.IsNullOrEmpty(vendor.VendorID))
                    {
                        try
                        {
                            if (VendorDA.CheckVendorIDUnique(dataBase, tran, vendor.VendorID, this.dataBaseID[0]))
                            {
                                if (!VendorDA.CheckAuthorizationVendorIDUserID(dataBase, tran, vendor.VendorID, this.dataBaseID[0], this.pubParasInfo))
                                {
                                    vendorIDFalse = vendorIDFalse + vendor.VendorID + PublicMethod.GetInstance().GetGlobalMsgResource("C008", this.pubParasInfo.UserLanguage) + "<br/>";
                                }
                                else if (IsUpdate == "Y")
                                {
                                    if (VendorDA.UpdateVendor(dataBase, tran, vendor, this.dataBaseID) > 0)
                                    {
                                        vendorIDFalse = vendorIDFalse + vendor.VendorID + "（数据已经更新）;<br/>";
                                    }
                                }
                                else
                                {
                                    vendorIDFalse = vendorIDFalse + vendor.VendorID + "（数据已经存在,导入失败）;<br/>";
                                }
                            }
                            else if (VendorDA.InsertVendor(dataBase, tran, vendor, this.dataBaseID) > 0)
                            {
                                vendorIDTrue = vendorIDTrue + vendor.VendorID + ";";
                            }
                        }
                        catch
                        {
                            if (!string.IsNullOrEmpty(vendor.VendorID))
                            {
                                vendorIDFalse = vendorIDFalse + vendor.VendorID + "（数据交互时出现异常,导入失败） ;<br/>";
                            }
                        }
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return vendorIDFalse;
        }
    }
}

